package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Depot;
import com.zyd.shiro.business.service.PakDepotService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakDepot;
import com.zyd.shiro.persistence.mapper.PakDepotMapper;
import lombok.extern.slf4j.Slf4j;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakDepotServiceImpl implements PakDepotService {

    @Autowired
    private PakDepotMapper pakDepotMapper;

    @Override
    public Depot insert(Depot depot) {
        Assert.notNull(depot, "对象不可为空！");
        depot.getPakDepot().setUser((Long) SecurityUtils.getSubject().getPrincipal());
        depot.getPakDepot().setUpdateTime(new Date());
        depot.getPakDepot().setCreateTime(new Date());
        pakDepotMapper.insertSelective(depot.getPakDepot());
        return depot;
    }

    @Override
    public void insertList(List<Depot> depots) {

    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakDepotMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Depot depot) {
        Assert.notNull(depot, "对象不可为空！");                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
        depot.setUpdateTime(new Date());
        return pakDepotMapper.updateByPrimaryKey(depot.getPakDepot()) > 0;
    }

    @Override
    public boolean updateSelective(Depot depot) {
        Assert.notNull(depot, "对象不可为空！");
        depot.setUpdateTime(new Date());
        return pakDepotMapper.updateByPrimaryKeySelective(depot.getPakDepot()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Depot getByPrimaryKey(Long primaryKey) {
        PakDepot pakDepot = pakDepotMapper.selectByPrimaryKey(primaryKey);
        return null == pakDepot ? null : new Depot(pakDepot);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Depot getOneByEntity(Depot test) {
        Assert.notNull(test, "对象不可为空！");
        PakDepot pakDepot = pakDepotMapper.selectOne(test.getPakDepot());
        return null == pakDepot ? null : new Depot(pakDepot);
    }

    @Override
    public List<Depot> listAll() {
        List<PakDepot> pakDepots = pakDepotMapper.selectAll();
        if (CollectionUtils.isEmpty(pakDepots)) {
            return null;
        }
        List<Depot> depots = new ArrayList<>();
        for (PakDepot pakDepot : pakDepots) {
            depots.add(new Depot(pakDepot));
        }
        return depots;
    }

    @Override
    public List<Depot> listByEntity(Depot entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Depot> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakDepot> pakDepots = pakDepotMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakDepots)) {
            return null;
        }
        List<Depot> depots = new ArrayList<>();
        for (PakDepot pakDepot : pakDepots) {
            depots.add(new Depot(pakDepot));
        }
        PageInfo bean = new PageInfo<PakDepot>(pakDepots);
        bean.setList(depots);
        return bean;
    }
    @Override
    public void increase(PakDepot pakDepot) {
        pakDepotMapper.insert(pakDepot);
    }




}
